package com.tao.service.impl;

import com.tao.common.pojo.EuTreeNode;
import com.tao.mapper.TbItemCatMapper;
import com.tao.pojo.TbItemCat;
import com.tao.pojo.TbItemCatExample;
import com.tao.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<EuTreeNode> getCatList(long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        List<EuTreeNode> euTreeNodes = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            EuTreeNode euTreeNode = new EuTreeNode();
            euTreeNode.setId(tbItemCat.getId());
            euTreeNode.setText(tbItemCat.getName());
            euTreeNode.setState(tbItemCat.getIsParent()?"closed":"open");
            euTreeNodes.add(euTreeNode);
        }
        return euTreeNodes;
    }
}
